/**
 * 
 */
package org.dragonfire.parser.functions;

import org.dragonfire.parser.Equation;

/**
 * @author Darth Android
 * @version 2.0
 */
public class Split extends Function {

	@Override
	public String eval(Equation equ, String[] params) {
		int index = equ.evalInt(params[2]);
		if (index < 0)
			return "\"\"";
		String regex = equ.evalString(params[1]);
		String text = equ.evalString(params[0]);
		String[] tokens = text.split(regex);
		if (index < 0 || index > tokens.length)
			return "\"\"";
		if (index == 0)
			return tokens.length + "";
		return "\"" + tokens[index - 1] + "\"";
	}

	@Override
	public int minParams() {
		return 3;
	}
}
